//引入网络库
import { get, post } from "../../util/util.js"

// 获取全局应用程序实例对象
var app = getApp();

// 创建页面实例对象
Page({
  /**
   * 页面名称
   */
  name: "pay",
  /**
   * 页面的初始数据
   */

  data: {
    uid: '',
    //上下拉
    newlist: {},
    pagesize: 1,
    loading: false,
    paydata:'',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
  },

  onLoad: function (o) {
    var that = this;
    that.setData({
      uid: o.uid,
    })
    post("/pay/GetProducts", {}).then(data => {
      console.log('pay/GetProducts====================' + JSON.stringify(data.data));
      // var dataJson = data.data;
      that.setData({
        newlist: data.data
      })
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },
  /**
   * 获取点击 身高 字段
   */
  clickItem: function (e) {
    var that = this;
    var index = parseInt(e.currentTarget.dataset.index); 
    that.setData({
      currentItemSg: index, 
      paydata: JSON.stringify(that.data.newlist[index])
    }) 
  },

  //以下为自定义点击事件
  goPay: function (e) {
    var that = this;
    var uid = that.data.uid;
    var pid = JSON.parse(that.data.paydata).pid;
    var price = JSON.parse(that.data.paydata).price;
    //pay/PreOrder 
    post("/pay/PreOrder", { uid: that.data.uid, platform: 1001, pid: pid }).then(data1 => {
      console.log("timeStamp========" + String(data1.data.timestamp))
      console.log("nonceStr========" + String(data1.data.noncestr))
      console.log("package========" + String(data1.data.package))
      console.log("signType========" + 'MD5')
      console.log("paySign========" + String(data1.data.sign))
      wx.requestPayment({
        'timeStamp': String(data1.data.timeStamp),
        'nonceStr': String(data1.data.nonceStr),
        'package': String(data1.data.package),
        'signType': String(data1.data.signType),
        'paySign': String(data1.data.sign), 
        // 'total_fee':price,
        'success': function (res) {
          console.log(res);
          wx.showModal({
            title: '提示',
            content: '充值成功',
            showCancel: false, //不显示取消按钮
            confirmText: '确定'
          })
        },
        'fail': function (res) {
          console.log('fail:' + JSON.stringify(res));
        }
      })

    })
  },

})

